package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.CheckGroupDao;
import com.itheima.dao.CheckItemDao;
import com.itheima.dao.SetMealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetMealService.class)
@Transactional
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private JedisPool jedisPool;
    @Autowired
    private SetMealDao setMealDao;
    @Autowired
    private CheckGroupDao checkGroupDao;
    @Autowired
    private CheckItemDao checkItemDao;

    //新增套餐
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        //先把数据插入数据库
        setMealDao.addSetMeal(setmeal);

        //然后根据最新获得的ID把所有的checkitemIds跟这个id关联在中间表里面
        Integer setMealId = setmeal.getId();
        if (checkgroupIds!=null && checkgroupIds.length>0){
            for (Integer checkgroupId : checkgroupIds) {
                Map<String,Integer> map=new HashMap<>();
                map.put("setMealId",setMealId);
                map.put("checkGroupId",checkgroupId);
                setMealDao.addReference(map);
            }
        }
        //如果数据被存储到数据库成功了,那么把该条数据的图片信息存储到redis里面,方便后续清理垃圾图片使用
        String fileName = setmeal.getImg();
        if (fileName!=null){
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,fileName);
        }

    }

    //分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);

        Page<Setmeal> page=setMealDao.findByCondition(queryString);
        long total = page.getTotal();
        List<Setmeal> rows = page.getResult();

        return new PageResult(total,rows);
    }

    //根据id查询套餐
    @Override
    public Setmeal findById(Integer id) {
        return setMealDao.findById(id);
    }

    //根据套餐id查询所有对应检查组的id
    @Override
    public List<Integer> findcheckgroupIds(Integer id) {
        return setMealDao.findcheckgroupIds(id);
    }

    //编辑套餐
    @Override
    public void update(Setmeal setmeal, Integer[] checkgroupIds) {
        //先把基础项重新设置
        setMealDao.update(setmeal);

        //然后根据id删除所有的关联
        Integer setMealId = setmeal.getId();
        setMealDao.deleteReference(setMealId);

        //重新绑定外界关联
        if (checkgroupIds!=null && checkgroupIds.length>0){
            for (Integer checkgroupId : checkgroupIds) {
                Map<String,Integer> map=new HashMap<>();
                map.put("setMealId",setMealId);
                map.put("checkGroupId",checkgroupId);
                setMealDao.addReference(map);
            }
        }

        //如果数据被存储到数据库成功了,那么把该条数据的图片信息存储到redis里面,方便后续清理垃圾图片使用
        String fileName = setmeal.getImg();
        if (fileName!=null){
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,fileName);
        }

    }

    //根据id删除套餐
    @Override
    public void deleteById(Integer id) {
        setMealDao.deleteById(id);
    }

    //查询所有的套餐
    @Override
    public List<Setmeal> getAllSetmeal() {
        return setMealDao.getAllSetmeal();
    }

    //根据id套餐id查询套餐
    @Override
    public Setmeal findSetmealById(Integer id) {

        Setmeal setmeal=setMealDao.newFindById(id);
        return setmeal;
    }

    @Override
    public List<Setmeal> findAll() {
        return setMealDao.findAll();
    }


    @Override
    public List<Integer> getCheckIds(Integer orderId) {
        return setMealDao.getCheckIds(orderId);
    }
}
